import mss
import redis
from time import time
from time import sleep
import os
from os import path
from pynput.keyboard import Key, Controller as c_keyboard
from pynput.mouse import Button, Controller as c_mouse


class Shoter():
    def __init__(self, redis_pool, filename = 'stockcode', rect = (0,0,1,1)) -> None:
        self.m = mss.mss()
        self.rect = rect
        self.redis_pool = redis_pool
        self.redis_conn = redis.Redis(connection_pool=self.redis_pool)
        self.filename = filename
        self.keyboard = c_keyboard()
    
    def get_conn(self):
        self.redis_conn = redis.Redis(connection_pool=self.redis_pool)
        
    def screenshot(self,tempdir='shot_image',filename = None):
        if not path.exists(tempdir):
            os.mkdir(tempdir)
        img = self.m.grab(self.rect)
        imgname = tempdir + '/' + self.filename + '_' + str(time()) + '.jpg'
        mss.tools.to_png(img.rgb, img.size, 6, imgname)
        if not self.redis_conn:
            self.get_conn()
        self.redis_conn.lpush('imgpath',imgname)
        self.keyboard.press(Key.left)
        self.keyboard.release(Key.left)
        sleep(0.2)
            